package com.shushan.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.shushan.manager.bean.RelaxTimeBean;
import com.shushan.util.DateUtils;
import com.shushan.util.ToolUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: RelaxTimeDao
 * @Desciption: 定义RelaxTimeBean数据访问对象，对指定的表进行增删改查操作
 * @author: yichaohua
 * @date: 2018-07-19
 */
public class RelaxTimeDao {

    private static final String TAG = "RelaxTimeDao";

    private Dao<RelaxTimeBean, Integer> relaxTimeDao;
    private DBHelper dbHelper;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     * @param context
     */
    public RelaxTimeDao(Context context) {
        try {
            dbHelper = DBHelper.getHelper(context);
            relaxTimeDao = dbHelper.getDao(RelaxTimeBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(relaxTimeDao == null){
            Log.e(TAG,"relaxTimeDao == null");
        }
    }

    /**
     * 添加一条记录
     * @param relaxTimeBean
     */
    public void add(RelaxTimeBean relaxTimeBean) {
        try {
            relaxTimeDao.create(relaxTimeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     * @param relaxTimeBean
     */
    public void delete(RelaxTimeBean relaxTimeBean) {
        try {
            relaxTimeDao.delete(relaxTimeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void update(int id,boolean isAction) {
        RelaxTimeBean relaxTimeBean = queryForId(id);
        if(relaxTimeBean != null){
            relaxTimeBean.isAction = isAction;

            try {
                relaxTimeDao.update(relaxTimeBean);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新一条记录
     * @param relaxTimeBean
     */
    public void update(RelaxTimeBean relaxTimeBean) {
        try {
            relaxTimeDao.update(relaxTimeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    public RelaxTimeBean queryForId(int id) {
        RelaxTimeBean relaxTimeBean = null;
        try {
            relaxTimeBean = relaxTimeDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return relaxTimeBean;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<RelaxTimeBean> queryForAll() {
        List<RelaxTimeBean> relaxTimeBeanList = new ArrayList<RelaxTimeBean>();
        try {
            relaxTimeBeanList = relaxTimeDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return relaxTimeBeanList;
    }

    public void clearTab(){
        try {
            relaxTimeDao.queryRaw("delete from relax_time_table");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public boolean isContainTime(int hour,int min){
        List<RelaxTimeBean> relaxTimeBeanList = queryForAll();
        if(ToolUtils.isListEmpty(relaxTimeBeanList)){
            return false;
        }

        for (RelaxTimeBean relaxTimeBean : relaxTimeBeanList){
            int time = hour * 60 + min;
            int relaxStartTime = relaxTimeBean.startHour * 60 + relaxTimeBean.startMin;
            int relaxEndTime = relaxTimeBean.endHour * 60 + relaxTimeBean.endMin;
            if ((time >= relaxStartTime) && (time <= relaxEndTime)){
                return relaxTimeBean.isAction;
            }
        }

        return false;
    }

    public boolean isContainWeek(){
        int weekIndex = DateUtils.getWeekIndex();

        List<RelaxTimeBean> relaxTimeBeanList = queryForAll();
        if(ToolUtils.isListEmpty(relaxTimeBeanList)){
            return false;
        }

        for (RelaxTimeBean relaxTimeBean : relaxTimeBeanList){
            int frequDay = relaxTimeBean.frequDay;
            if((frequDay & (1<<weekIndex)) > 0){
                return true;
            }
        }

        return false;
    }
}
